// Copyright (c) 2014 Converter Systems LLC

using ConverterSystems.Workstation.Infrastructure;
using ConverterSystems.Workstation.ViewModels;
using MahApps.Metro.Controls.Dialogs;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Regions;
using System;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Windows.Input;

namespace ConverterSystems.Workstation.Views
{
    /// <summary>
    /// Interaction logic for Shell.xaml
    /// </summary>
    [Export]
    public sealed partial class Shell
    {
        public static RoutedUICommand About = new RoutedUICommand("About", "About", typeof(Shell));
        public static RoutedUICommand Settings = new RoutedUICommand("Settings", "Settings", typeof(Shell));
        public static RoutedUICommand SignIn = new RoutedUICommand("SignIn", "SignIn", typeof(Shell));

        public Shell()
        {
            InitializeComponent();
        }

        [ImportingConstructor]
        public Shell(ShellViewModel viewModel, IRegionManager regionManager)
            : this()
        {
            DataContext = viewModel;
            RegionManager = regionManager;
        }

        public IRegionManager RegionManager { get; private set; }

        private async void OnAboutExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            e.Handled = true;
            await this.ShowMetroDialogAsync(new AboutWorkstation(this, new MetroDialogSettings { ColorScheme = MetroDialogColorScheme.Accented }));
        }

        private void OnBrowseBackCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = RegionManager.Regions[RegionNames.MainContent].NavigationService.Journal.CanGoBack;
            e.Handled = true;
        }

        private void OnBrowseBackExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            RegionManager.Regions[RegionNames.MainContent].NavigationService.Journal.GoBack();
            e.Handled = true;
        }

        private void OnGoToPageExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            var uri = new Uri(e.Parameter as string, UriKind.Relative);
            var target = UriParsingHelper.GetAbsolutePath(uri);
            var parameters = UriParsingHelper.ParseQuery(uri);
            RegionManager.RequestNavigate(RegionNames.MainContent, target, parameters);
            e.Handled = true;
        }

        private void OnSettingsExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            SignInFlyout.IsOpen = false;
            SettingsFlyout.IsOpen = true;
            e.Handled = true;
        }

        private void OnSignInExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            SettingsFlyout.IsOpen = false;
            SignInFlyout.IsOpen = true;
            e.Handled = true;
        }
    }
}